package com.yunxin.facerecognition.activity;

import android.Manifest;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.common.activity.BaseActivity;
import com.mukesh.permissions.AppPermissions;
import com.yunxin.facerecognition.R;
import com.yunxin.facerecognition.databinding.FaceRecognitionActivityMainBinding;


/**
 * author : Administrator
 * e-mail : 254419013@qq.com
 * time   : 2017/11/13
 * desc   :
 */
public class MainActivity extends BaseActivity {

    FaceRecognitionActivityMainBinding binding;

    private static final String[] ALL_PERMISSIONS = {
            Manifest.permission.SEND_SMS,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,//摄像头
            Manifest.permission.RECORD_AUDIO,//音频
            Manifest.permission.VIBRATE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.WRITE_SETTINGS,
            Manifest.permission.READ_PHONE_STATE,//读取手机状态
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS,
            Manifest.permission.READ_CALL_LOG,
            Manifest.permission.WRITE_CALL_LOG,
            Manifest.permission.READ_SMS,
            Manifest.permission.ACCESS_NOTIFICATION_POLICY,
            Manifest.permission.SYSTEM_ALERT_WINDOW
    };
    private static final int ALL_REQUEST_CODE = 0;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                   /* WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
                    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                    params.type = WindowManager.LayoutParams.TYPE_PHONE;
                    params.format = PixelFormat.RGBA_8888;
                    windowManager.addView(view, params);*/
                } else {
                    Toast.makeText(this, "ACTION_MANAGE_OVERLAY_PERMISSION权限已被拒绝", Toast.LENGTH_SHORT).show();
                    ;
                }
            }

        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(!Settings.canDrawOverlays(getApplicationContext())) {
                //启动Activity让用户授权
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.setData(Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent,100);
            }
        }

        binding = DataBindingUtil.setContentView(this, R.layout.face_recognition_activity_main);
        initListener();
        initPermissions();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initListener() {
        binding.faceRecognitionActivityMainBtnRegistration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), RegistrationActivity.class);
                startActivity(intent);
            }
        });

        binding.faceRecognitionActivityMainBtnVerifySignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), DetectFaceActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initPermissions() {
        AppPermissions runtimePermission = new AppPermissions(this);
        runtimePermission.requestPermission(ALL_PERMISSIONS,
                ALL_REQUEST_CODE);

    }
}
